package com.hylanda.pb.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年1月19日 上午10:29:42 
 * note
 */
public abstract class SqlServerBaseDao {
	@Resource(name = "sqlseverJdbcTemplate")
	public JdbcTemplate template;
	public  Logger logger =LoggerFactory.getLogger(getClass());
	/**
	 * 执行查询
	 * @param sql
	 * @return List<Map<String, Object>>
	 * @throws DataAccessException
	 */
	public List<Map<String, Object>> queryForList(String sql) throws DataAccessException{
		return this.template.queryForList(sql);
	}
	
	/**
	 * 执行查询
	 * @param sql
	 * @param args
	 * @return List<Map<String, Object>>
	 * @throws DataAccessException
	 */
	public List<Map<String, Object>> queryForList(String sql, Object... args) throws DataAccessException{
		return this.template.queryForList(sql, args);
	}
	
	/**
	 * 查询单列
	 * @param sql
	 * @param claz
	 * @return
	 */
	public <T> List<T> queryForSingleClum(String sql, Class<T> claz) throws DataAccessException{
		RowMapper<T> rowMapper = new SingleColumnRowMapper<T>();
		return this.template.query(sql, rowMapper);
	}
	
	/**
	 * 查询单列
	 * @param sql
	 * @param claz
	 * @return
	 */
	public <T> List<T> queryForSingleClum(String sql, Class<T> claz, Object... args) throws DataAccessException{
		RowMapper<T> rowMapper = new SingleColumnRowMapper<T>();
		return this.template.query(sql, rowMapper, args);
	}
	/**
	 * 查询单列单值
	 * @param sql
	 * @param claz
	 * @param args
	 * @return
	 */
	public <T> T queryforSingleClumSValue(String sql,Class<T> claz,Object... args){
		RowMapper<T> rowMapper = new SingleColumnRowMapper<T>();
		if(!sql.contains("limit 0,1"))
			sql=sql+" limit 0,1";
		return this.template.queryForObject(sql, rowMapper,args);
	}
	/**
	 * 执行查询
	 * @param sql
	 * @param claz
	 * @param args
	 * @return
	 */
	public <T> List<T> query(String sql, Class<T> claz, Object... args) throws DataAccessException{
		RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(claz);
		return this.template.query(sql, rowMapper, args);
	}
	
	/**
	 * 执行查询
	 * @param sql
	 * @param claz
	 * @param args
	 * @return
	 */
	public <T> List<T> query(String sql, Class<T> claz) throws DataAccessException{
		RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(claz);
		return this.template.query(sql, rowMapper);
	}
	
	/**
	 * 执行Count查询
	 * @param countSql
	 * @return
	 */
	public int queryCount(String countSql) throws DataAccessException{
		Integer number = (Integer)queryForObject(countSql);
		 int count =(number != null ? number.intValue() : 0);
		 return count;
	}
	
	/**
	 * 查询特定类型
	 * @param sql
	 * @param claz
	 * @return
	 */
	public <T> T queryForObject(String sql, Class<T> claz) throws DataAccessException{
		RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(claz);
		return template.queryForObject(sql, rowMapper);
	}
	public <T> T queryForObject(String sql) throws DataAccessException{
		RowMapper<T> rowMapper = new SingleColumnRowMapper<T>();
		return template.queryForObject(sql, rowMapper);
	}
	/**
	 * 执行新增修改及删除
	 * @param sql
	 * @return
	 */
	public int executeUpdate(String sql) throws DataAccessException{
		return template.update(sql);
		
	};
	
	/**
	 * 执行新增修改
	 * @param sql
	 * @param param
	 * @return
	 */
	public int executeUpdate(String sql, Object... param) throws DataAccessException{
		return template.update(sql, param);
	};
	
}
